﻿Imports System.Runtime.InteropServices
Imports Microsoft.Feeds.Interop

Module MainModule
    Friend UnreadRSSItems As New List(Of IFeedItem)
    Friend RSSItemForms As New List(Of RSSItemForm)
    Friend ClosingApp As Boolean = False
    Friend FeedsManager As New FeedsManager

#Region "NotifyIcon"
    Friend WithEvents NotifyIcon As System.Windows.Forms.NotifyIcon
    Friend WithEvents ContextMenuStrip As System.Windows.Forms.ContextMenuStrip
    Friend WithEvents CloseToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents RefreshNowToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
    Friend WithEvents ManageRSSFeedsToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem

    Sub ShowNotifyIcon()
        'RefreshNowToolStripMenuItem
        '
        RefreshNowToolStripMenuItem = New ToolStripMenuItem
        RefreshNowToolStripMenuItem.Name = "RefreshNowToolStripMenuItem"
        RefreshNowToolStripMenuItem.Size = New System.Drawing.Size(172, 22)
        RefreshNowToolStripMenuItem.Text = "Refresh Now"
        '
        'ManageRSSFeedsToolStripMenuItem
        '
        ManageRSSFeedsToolStripMenuItem = New ToolStripMenuItem
        ManageRSSFeedsToolStripMenuItem.Name = "ManageRSSFeedsToolStripMenuItem"
        ManageRSSFeedsToolStripMenuItem.Size = New System.Drawing.Size(172, 22)
        ManageRSSFeedsToolStripMenuItem.Text = "Manage RSS Feeds"
        '
        'CloseToolStripMenuItem
        '
        CloseToolStripMenuItem = New ToolStripMenuItem
        CloseToolStripMenuItem.Name = "CloseToolStripMenuItem"
        CloseToolStripMenuItem.Size = New System.Drawing.Size(172, 22)
        CloseToolStripMenuItem.Text = "Close"
        '
        'ContextMenuStrip
        '
        ContextMenuStrip = New ContextMenuStrip
        ContextMenuStrip.Items.AddRange(New System.Windows.Forms.ToolStripItem() {RefreshNowToolStripMenuItem, ManageRSSFeedsToolStripMenuItem, CloseToolStripMenuItem})
        ContextMenuStrip.Name = "ContextMenuStrip"
        ContextMenuStrip.Size = New System.Drawing.Size(173, 70)
        '
        'NotifyIcon
        '
        NotifyIcon = New NotifyIcon
        NotifyIcon.ContextMenuStrip = ContextMenuStrip
        NotifyIcon.Icon = My.Resources.icon
        NotifyIcon.Text = "RSS Feed Popper"
        NotifyIcon.Visible = True
    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        ClosingApp = True
    End Sub

    Private Sub RefreshNowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshNowToolStripMenuItem.Click
        UpdateRSSForm.ShowDialog()
    End Sub

    Private Sub ManageRSSFeedsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ManageRSSFeedsToolStripMenuItem.Click
        Options.Show()
    End Sub

    Friend SafeShowMessage As ShowMessage
    Delegate Sub ShowMessage(ByVal MessageString As String)
    Sub _ShowMessage(ByVal MessageString As String)
        NotifyIcon.ShowBalloonTip(5000, "RSS Feed Popper", MessageString, ToolTipIcon.Info)
    End Sub
#End Region

    <DllImport("dwmapi.dll", preservesig:=False)> _
    Public Function DwmIsCompositionEnabled() As Boolean
    End Function

    Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(True)
        ShowNotifyIcon()
        SafeShowMessage = New ShowMessage(AddressOf _ShowMessage)
        Try
            If DwmIsCompositionEnabled() = False Then
                NotifyIcon.ShowBalloonTip(5000, "RSS Feed Popper", "RSS Feed Popper works best on Vista with Aero turned on", ToolTipIcon.Info)
            End If
        Catch
        End Try
        Dim MainLoopThread As New Threading.Thread(AddressOf MainLoop)
        MainLoopThread.Start()
        Application.Run()
    End Sub

    Sub MainLoop()
        Do While ClosingApp = False
            FeedsManager.BackgroundSync(FEEDS_BACKGROUNDSYNC_ACTION.FBSA_RUNNOW)
            GetUnreadRSSFeeds()
            For x As Integer = 1 To 60
                If ClosingApp = True Then GoTo exitline
                If RSSItemForms.Count < 4 Then
                    For i = RSSItemForms.Count To 3
                        If UnreadRSSItems.Count > i Then
                            Dim CreateNewFormThread As New Threading.Thread(AddressOf CreateNewForm)
                            Try
                                CreateNewFormThread.Start(UnreadRSSItems(i))
                                UnreadRSSItems.RemoveAt(i)
                            Catch ex As Exception
                                SafeShowMessage("Could not show item" & vbCr & ex.Message)
                            End Try
                        End If
                    Next
                End If
                If RSSItemForms.Count > 0 Then
                    For i = 0 To RSSItemForms.Count - 1
                        Dim ypos As Integer = My.Computer.Screen.WorkingArea.Height / 2
                        ypos += (((RSSItemForms.Count / 2) - i - 1) * 175)
                        RSSItemForms(i).TargetLocation = New Point(My.Computer.Screen.WorkingArea.Width - 120, ypos)
                    Next
                End If
                System.Threading.Thread.Sleep(1000)
            Next
            My.Settings.Save()
        Loop
exitline:
        My.Settings.Save()
        Try
            Application.Exit()
        Catch
        End Try
    End Sub

    Sub CreateNewForm(ByVal Item As IFeedItem)
        Try
            Item.IsRead = True
            Dim RSSItemForm As New RSSItemForm(Item)
            RSSItemForms.Add(RSSItemForm)
            Dim ypos As Integer = My.Computer.Screen.WorkingArea.Height / 2
            ypos += (((RSSItemForms.Count / 2) - RSSItemForms.IndexOf(RSSItemForm) - 1) * 175)
            RSSItemForm.TargetLocation = New Point(My.Computer.Screen.WorkingArea.Width + 20, ypos)
            RSSItemForm.Location = New Point(My.Computer.Screen.WorkingArea.Width + 20, ypos)
            RSSItemForm.ShowDialog()
        Catch ex As Exception
            Debug.Print(ex.Message)
        End Try
    End Sub

    Sub GetUnreadRSSFeeds()
        Try
            Dim Feeds As List(Of IFeed) = GetFeeds(FeedsManager.RootFolder)
            For Each Feed As IFeed In Feeds
                If Feed.UnreadItemCount > 0 Or Debugger.IsAttached = True Then
                    For Each item As IFeedItem In Feed.Items
                        If item.PubDate < Today.AddDays(-2) And Debugger.IsAttached = False Then GoTo nextline
                        If item.IsRead = False Or Debugger.IsAttached = True Then
                            For Each unreaditem As IFeedItem In UnreadRSSItems
                                If unreaditem.Title = item.Title Then GoTo nextline
                            Next
                            UnreadRSSItems.Add(item)
                        End If
nextline:
                    Next
                End If
            Next
        Catch
            GetUnreadRSSFeeds()
        End Try
    End Sub

    Function GetFeeds(ByVal CurrentFolder As IFeedFolder) As List(Of IFeed)
        Dim Feeds As New List(Of IFeed)
        For Each Feed As IFeed In CurrentFolder.Feeds
            Feeds.Add(Feed)
        Next
        For Each Folder As IFeedFolder In CurrentFolder.Subfolders
            For Each item As IFeed In GetFeeds(Folder)
                Feeds.Add(item)
            Next
        Next
        Return Feeds
    End Function
End Module
